﻿Imports BUS
Imports DTO
Public Class UserControlPhieuRutTien

    Private Sub UserControlPhieuRutTien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Load ma so len combobox
        Dim stkBUS As New SoTietKiemBUS()
        Dim dsstk As New List(Of SoTietKiemDTO)()
        dsstk = stkBUS.LaySoTietKiem()
        cbbMaSo.DataSource = dsstk
        'cbbMaSo.DisplayMember = "MaSo"
        cbbMaSo.ValueMember = "MaSo"
        'Load khach hang len textbox
        Dim maso As Integer = cbbMaSo.SelectedValue
        Dim khBUS As New KhachHangBUS()
        Dim khDTO As New KhachHangDTO()
        khDTO = khBUS.LayKhachHangTheoMaSo(maso)
        txtKhachHang.Text = khDTO.MaKH.ToString()

        Dim prtBUS As New PhieuRutTienBUS()
        txtMaPhieuRut.Text = (prtBUS.LayMaPhieuRutMax() + 1).ToString()
    End Sub

    Private Sub cbbMaSo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim maso As Integer = cbbMaSo.SelectedValue
        Dim khBUS As New KhachHangBUS()
        Dim khDTO As New KhachHangDTO()
        khDTO = khBUS.LayKhachHangTheoMaSo(maso)
        txtKhachHang.Text = khDTO.MaKH.ToString()
    End Sub

    Private Sub btnDongY_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDongY.Click
        Dim stkBUS As New SoTietKiemBUS()
        Dim stkDTO As New SoTietKiemDTO()
        stkDTO = stkBUS.TimKiemTheoMaSo(cbbMaSo.SelectedValue)
        Dim ngaymoso As Date = stkDTO.NgayMoSo.Date
        Dim ngayrut As Date = dtpNgayRut.Value.Date
        If ThamSoBUS.KiemTraNgayRut(ngayrut, ngaymoso) = False Then
            MessageBox.Show("Chua toi ngay rut")
            Return
        End If
        Dim temp As Double = 0
        If Double.TryParse(txtSoTienRut.Text, temp) = False Or temp < 0 Then
            MessageBox.Show("Số tiền rút không hợp lệ")
            Return
        End If
        Dim prtBUS As New PhieuRutTienBUS()
        Dim prtDTO As New PhieuRutTienDTO()
        prtDTO.MaSo = cbbMaSo.SelectedValue
        prtDTO.MaKH = txtKhachHang.Text
        prtDTO.NgayRut = dtpNgayRut.Value
        prtDTO.SoTienRut = txtSoTienRut.Text


        Dim sodu As Double = stkBUS.LaySoDuTaiKhoan(prtDTO.MaSo)

        If temp > sodu Then
            MessageBox.Show("Số tiền bạn muốn rút lơn hơn số dư trong tài khoản")
            Return
        End If
        If temp = sodu Then
            Dim ngayDong As Date = Date.Now.Date
            stkBUS = New SoTietKiemBUS()
            stkBUS.CapNhaMTTSTK(2, ngayDong, prtDTO.MaSo)
        End If
        stkBUS = New SoTietKiemBUS()
        Dim kq As Integer = prtBUS.ThemPhieuRutTien(prtDTO)
        If kq = 1 Then

            kq = stkBUS.SoDuTaiKhoanSauKhiRut(prtDTO.MaSo, prtDTO.SoTienRut)
            If kq = 1 Then
                MessageBox.Show("Rút thành công")
                txtMaPhieuRut.Text = (prtBUS.LayMaPhieuRutMax() + 1).ToString()
            Else
                MessageBox.Show("Rút thất bại")
            End If
        Else
            MessageBox.Show("Rút thất bại")
            Return
        End If
    End Sub


    Private Sub cbbMaSo_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbMaSo.SelectedIndexChanged
        Dim maso As Integer = cbbMaSo.SelectedValue
        Dim khBUS As New KhachHangBUS()
        Dim khDTO As New KhachHangDTO()
        khDTO = khBUS.LayKhachHangTheoMaSo(maso)
        txtKhachHang.Text = khDTO.MaKH.ToString()
        Dim stkBUS As New SoTietKiemBUS()
        Dim prtDTO As New PhieuRutTienDTO()
        prtDTO.MaSo = cbbMaSo.SelectedValue
        Dim sodu As Double = stkBUS.LaySoDuTaiKhoan(prtDTO.MaSo)
        txtSoTienRut.Text = sodu.ToString()
    End Sub

 
End Class
